package kr.hkit.powerjava.chapter17;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class TablePanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private JButton btnAdd;
	private JTable table;
	private DecimalFormat df=new DecimalFormat("#,###");
	

	public TablePanel() {
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		JPanel panelTop = new JPanel();
		add(panelTop, BorderLayout.NORTH);

		btnAdd = new JButton("추가");
		btnAdd.addActionListener(this);
		panelTop.add(btnAdd);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);

		String colNames[] = { "사원번호", "성명", "직책", "매니저", "급여", "부서번호" };

		TableModel model = new DefaultTableModel(colNames, 0) {
			private static final long serialVersionUID = 1L;
			public Class<?> getColumnClass(int column) {
				return getValueAt(0, column).getClass();
			}
		};

		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
	            int row = table.getSelectedRow();
	            int col = table.getSelectedColumn();
	            System.out.println(table.getModel().getValueAt(row, col));   // 눌려진 행과 열에 해당하는 선택된 데이터 하나 출력
	            
	            //선택된 한 row출력
	            for(int i=0;i<table.getColumnCount(); i++){
	            	System.out.printf("%s "	, table.getModel().getValueAt(row, i));
	            }
	            System.out.println();
			}
		});
		
		Object[] cls = { 
				String.valueOf((int)(Math.random()*10)+1),
				new String("김태희"), 
				new String("사원"),
				new Integer((int)(Math.random()*10)+1),
				df.format(new Integer(10000000)), 
				String.valueOf((int)(Math.random()*10)+1)
		};

		((DefaultTableModel) table.getModel()).addRow(cls);
		
		scrollPane.setViewportView(table);
		
		tableCellWidthAndAlign();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}

	protected void actionPerformedBtnAdd(ActionEvent e) {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		
		Object[] newRow = {
				new Integer((int) (Math.random() * 10) + 1), 
				"김태희", 
				"사원",
				new Integer((int) (Math.random() * 10) + 1),
				df.format(new Integer(10000000)),
				new Integer((int) (Math.random() * 10) + 1)
		};
		
		model.addRow(newRow);

	}

	private void tableCellWidthAndAlign() {
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		DefaultTableCellRenderer dtcrRighter = new DefaultTableCellRenderer();
		dtcrRighter.setHorizontalAlignment(SwingConstants.RIGHT);
		
		TableColumnModel tcm = table.getColumnModel();
		
		int len = tcm.getColumnCount();
		
		for(int i=0; i<len; i++){
			if (i == 4){
				tcm.getColumn(i).setPreferredWidth(100);
				tcm.getColumn(i).setCellRenderer(dtcrRighter);
			}else if (table.getColumnClass(i).equals(String.class)){
				tcm.getColumn(i).setPreferredWidth(70);
				tcm.getColumn(i).setCellRenderer(dtcrCenter);
			}
		}		
	}
	
}
